<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use app\service\GoodsService;
use app\service\OuterPurchaseService;
use app\service\UserService;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Request;

class StoreOuterController extends BaseController
{
    /**
     * 获取外部采购列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', env('pager.default.size'));
      $base = $request->param('base', []);
      $type = $request->param('type', '');
      $conditions = [];
      if ($base === '') {
        return $this->send400Response('base必填');
      }
      if($base){
        $conditions[] = ['s.base', $base, 'IN'];
      }
      $list = OuterPurchaseService::getInstance()->getOuterJoinStore($conditions);
      $list = array_values(array_column($list, null, 'sell_number'));
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);
    }

    /**
     * 批量新增外部采购
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $data = $request->param('data', []);
      $base = $request->param('base', '');
      $sure = $request->param('sure', false);
      if (!$data) {
        return $this->send400Response('缺少data参数');
      }
      if ($base === '') {
        return $this->send400Response('缺少base参数');
      }
      $result = OuterPurchaseService::getInstance()->createMultiOuter($base, $data, $sure);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response($result['msg']);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 根据编号获取外部采购详情
     * @return \think\Response
     */
    public function getByNum(Request $request){
      $sellNum = $request->param('sell_number','');
      if (!$sellNum) {
        return $this->send400Response('订单号不能为空');
      }
      $conditions[] = ['o.sell_number', $sellNum, '='];
      $outer = OuterPurchaseService::getInstance()->getOuterJoinStore($conditions);
      $data = [
        'purchase_time' => '',
        'list' => []
      ];
      if (isset($outer[0])) {
        $data['purchase_time'] = $outer[0]['purchase_time'];
        $data['list'] = $outer;
      }
      return $this->send200Response($data);
    }

    /**
     * 导出外部采购明细
     * @return \think\Response
     */
    public function export(Request $request) {
      $base = $request->param('base', []);
      $conditions = [];
      if($base){
        $conditions[] = ['s.base', $base, 'IN'];
      }
      $list = OuterPurchaseService::getInstance()->getOuterJoinStore($conditions);
      $spreadsheet = new Spreadsheet();
      $sheet = $spreadsheet->getActiveSheet();
      if ($base) {
        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(15);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('D')->setWidth(15);
        $sheet->getColumnDimension('E')->setWidth(15);
        $sheet->getColumnDimension('F')->setWidth(15);
        $sheet->getColumnDimension('G')->setWidth(15);
        $sheet->getColumnDimension('H')->setWidth(20);

        $sheet->setCellValue('A1', '采购单号');
        $sheet->setCellValue('B1', '日期');
        $sheet->setCellValue('C1', '分类');
        $sheet->setCellValue('D1', '商品名称');
        $sheet->setCellValue('E1', '采购数量');
        $sheet->setCellValue('F1', '采购单位');
        $sheet->setCellValue('G1', '采购成本');
        $sheet->setCellValue('H1', '总价');
        $sheet->setCellValue('I1', '备注');
        $row = 1;
        foreach($list as $v){
          $row++;
          $sheet->setCellValue("A{$row}", " ".$v['sell_number']);
          $sheet->setCellValue("B{$row}", date('Y-m-d', strtotime($v['purchase_time'])));
          $sheet->setCellValue("C{$row}", $v['category_name']);
          $sheet->setCellValue("D{$row}", $v['name']);
          $sheet->setCellValue("E{$row}", $v['count']);
          $sheet->setCellValue("F{$row}", $v['unit']);
          $sheet->setCellValue("G{$row}", $v['price']);
          $sheet->setCellValue("H{$row}", $v['count']*$v['price']);
          $sheet->setCellValue("I{$row}", $v['comment']);
        }
      } else {
        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(15);
        $sheet->getColumnDimension('C')->setWidth(15);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(15);
        $sheet->getColumnDimension('F')->setWidth(15);
        $sheet->getColumnDimension('G')->setWidth(15);
        $sheet->getColumnDimension('H')->setWidth(15);
        $sheet->getColumnDimension('I')->setWidth(20);

        $sheet->setCellValue('A1', '采购单号');
        $sheet->setCellValue('B1', '流向');
        $sheet->setCellValue('C1', '日期');
        $sheet->setCellValue('D1', '商品名称');
        $sheet->setCellValue('E1', '采购数量');
        $sheet->setCellValue('F1', '采购单位');
        $sheet->setCellValue('G1', '采购成本');
        $sheet->setCellValue('H1', '总价');
        $sheet->setCellValue('I1', '备注');
        $row = 1;
        foreach($list as $v){
          $row++;
          $sheet->setCellValue("A{$row}", " ".$v['sell_number']);
          $sheet->setCellValue("B{$row}", $v['basename']);
          $sheet->setCellValue("C{$row}", date('Y-m-d', strtotime($v['purchase_time'])));
          $sheet->setCellValue("D{$row}", $v['name']);
          $sheet->setCellValue("E{$row}", $v['count']);
          $sheet->setCellValue("F{$row}", $v['unit']);
          $sheet->setCellValue("G{$row}", $v['price']);
          $sheet->setCellValue("H{$row}", $v['count']*$v['price']);
          $sheet->setCellValue("I{$row}", $v['comment']);
        }
      }

      header("Content-type: application/octet-stream;charset=utf-8");
      header('Content-Disposition: attachment;filename="采购明细导出.xlsx"');
      header('Cache-Control: max-age=0');
      $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
      $writer->save('php://output');
    }
}
